# -*- coding: utf-8 -*-
# @FileName : Seller.py
# @Desc     :
# @Time     : 2024/5/5 17:57
# @Author   : Runke Zhong
from common.libs.pay.PayService import PayService
from web.controller.api import route_api
from common.models.Goods import Goods
from flask import jsonify, request
from common.models.User import User
from application import db, app
from common.libs.UrlManager import UrlManager
from common.models.Order import Order
from common.models.OrderItem import OrderItem
from common.libs.Helper import selectFilterObj, getDictFilterField, getCurrentDate


# /seller/noDeliverGoods


@route_api.route("/seller/noDeliverGoods", methods=["POST"])
def noDeliverGoods():
    """
    不发货
    :return:
    """
    resp = {'code': 200, 'msg': 'OK', 'data': {}}
    req = request.values
    print(req)
    token = req['token'] if 'token' in req else None
    order_sn = req['order_sn'] if 'order_sn' in req else None
    if not all([token, order_sn]):
        resp['code'] = -1
        resp['msg'] = '参数不正确'
        return jsonify(resp)
    try:
        auth_info = token.split("#")
        user_info = User.query.filter_by(id=auth_info[1]).first()
    except Exception:
        resp['code'] = -1
        resp['msg'] = '用户异常'
        return jsonify(resp)
    if not user_info:
        resp['code'] = -1
        resp['msg'] = '用户异常'
        return jsonify(resp)
    # 判断逻辑
    """
    卖家必须为当前用户
    订单状态必须为待发货，即order.status=1, express_status=-7
    修改为无需发货,即order.status=1, express_status=-6
    可能会有问题，比如怎么去区分无需发货和已发货
    """
    the_order = Order.query.filter_by(seller=user_info.address,
                                  order_sn=order_sn,
                                  status=1,
                                  express_status=-7).first()
    the_order.status = 1
    the_order.express_status = -6
    try:
        db.session.add(the_order)
        db.session.commit()
    except:
        resp['code'] = -1
        resp['msg'] = '数据库错误'
        return jsonify(resp)
    return jsonify(resp)


@route_api.route("/seller/ops", methods=["POST"])
def opsOrder():
    """
    取消订单
    :return:
    """
    resp = {'code': 200, 'msg': 'OK', 'data': {}}
    # 登录校验
    auth_cookie = request.headers.get("Authorization") if 'Authorization' in request.headers else ''
    if not auth_cookie:
        resp['code'] = -1
        resp['msg'] = '没有Authorization'
        return jsonify(resp)
    auth_info = auth_cookie.split("#")
    print(auth_info)
    user_info = User.query.filter_by(id=auth_info[-1], status=1).first()
    if not user_info:
        resp['code'] = -1
        resp['msg'] = '没有此用户'
        return jsonify(resp)
    req = request.values
    order_sn = req['order_sn'] if 'order_sn' in req else ''
    act = req['act'] if 'act' in req else ''
    print(act)
    order_info = Order.query.filter_by(order_sn=order_sn, seller=user_info.address).first()
    if not order_info:
        resp['code'] = -1
        resp['msg'] = '系统繁忙，请稍后再试'
        return jsonify(resp)
    if act == 'cancel':
        target_pay = PayService()
        ret = target_pay.closeOrder(order_id=order_info.id)
        if not ret:
            resp['code'] = -1
            resp['msg'] = '系统繁忙，请稍后再试'
            return jsonify(resp)
    elif act == 'confirm':
        order_info.express_status = 1
        order_info.updated_time = getCurrentDate()
        db.session.add(order_info)
        db.session.commit()
    return jsonify(resp)